//
//  WaterRippleWait.m
//  DGActivityIndicatorExample
//
//  Created by 123 on 16/5/23.
//  Copyright © 2016年 Danil Gontovnik. All rights reserved.
//

#import "WaterRippleWait.h"
#import "MBProgressHUD+MJ.h"

#define DefaultBackgroundColor [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]
#define DefaultWaterColor [UIColor redColor]

@interface WaterRippleWait()

@property (nonatomic,assign) BOOL isReadyRemove;

@end

@implementation WaterRippleWait


+ (instancetype)water
{
    WaterRippleWait *water = [[WaterRippleWait alloc]init];
    water.backgroundColor = DefaultBackgroundColor;
    
    UIView *animatedView = [[UIView alloc]init];
    [water addSubview:animatedView];
    return water;
}
//---------------显示方法---------------
+ (void)waterRippleShow
{
    [self waterRippleShowInView:nil];
}
+ (void)waterRippleShowBackgroundColor:(UIColor *)color
{
    [self waterRippleShowInView:nil backgroundColor:color];
}
+ (void)waterRippleShowInView:(UIView *)view
{
    [self waterRippleShowInView:view backgroundColor:DefaultBackgroundColor];
}
+ (void)waterRippleShowInView:(UIView *)view backgroundColor:(UIColor *)color
{
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    WaterRippleWait *water = [WaterRippleWait water];
    water.frame = view.bounds;
    water.backgroundColor = color;
    
    UIView *animatedView = water.subviews.firstObject;
    animatedView.frame = CGRectMake((view.frame.size.width-60)/2.0, (view.frame.size.height-60)/2.0, 60, 60);
    [view addSubview:water];
    [water setupAnimation];
}

//---------------隐藏方法---------------
+ (void)hideWaterRipple
{
    [self hideWaterRippleInView:nil];
}
+ (void)hideWaterRippleInView:(UIView *)view
{
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    
    WaterRippleWait * water = [self getWaterRippleWithView:view];
    UIView *animationView = water.subviews.firstObject;
    if (water!=nil) {
        water.isReadyRemove = YES;
        [UIView animateWithDuration:0.5 animations:^{
            animationView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.01, 0.01);
        } completion:^(BOOL finished) {
            [water removeFromSuperview];
            [water.layer removeAllAnimations];
            animationView.transform = CGAffineTransformIdentity;
        }];
    }
}
//获取water方法
+ (WaterRippleWait *)getWaterRippleWithView:(UIView *)view
{
    NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
    for (UIView *subview in subviewsEnum) {
        if ([subview isKindOfClass:self]) {
            WaterRippleWait *water = (WaterRippleWait *)subview;
            if (water.isReadyRemove) {
                continue;
            }
            return water;
        }
    }
    return nil;
}
//水波纹动画
- (void)setupAnimation
{
//    UIView *animatedView = self.subviews.firstObject;
//    
//    NSTimeInterval beginTime = CACurrentMediaTime();
//    for (NSInteger i=0; i < 2; i+=1) {
//        CALayer *circle = [CALayer layer];
//        circle.frame = CGRectInset(animatedView.bounds, 2.0, 2.0);
//        if (self.showColor) {
//            circle.backgroundColor = self.showColor.CGColor;
//        }else{
//            UIColor *color = self.showColor?self.showColor:DefaultWaterColor;
//            circle.backgroundColor = color.CGColor;
//        }
//        circle.anchorPoint = CGPointMake(0.5, 0.5);
//        circle.opacity = 0.6;
//        circle.cornerRadius = CGRectGetHeight(circle.bounds) * 0.5;
//        circle.transform = CATransform3DMakeScale(0.0, 0.0, 0.0);
//        
//        CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
//        anim.removedOnCompletion = NO;
//        anim.repeatCount = HUGE_VALF;
//        anim.duration = 2.0;
//        anim.beginTime = beginTime - (1.0 * i);
//        anim.keyTimes = @[@(0.0), @(0.5), @(1.0)];
//        
//        anim.timingFunctions = @[
//                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
//                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
//                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
//                                 ];
//        
//        anim.values = @[
//                        [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.0, 0.0, 0.0)],
//                        [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 0.0)],
//                        [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.0, 0.0, 0.0)]
//                        ];
//        [animatedView.layer addSublayer:circle];
//        [circle addAnimation:anim forKey:@"spinkit-anim"];
//    }
    
    UIView *animatedView = self.subviews.firstObject;
    NSTimeInterval beginTime = CACurrentMediaTime();
    CGSize size = animatedView.frame.size;
    for (int i = 0; i < 4; i++) {
        CALayer *circle = [CALayer layer];
        circle.frame = CGRectMake(0, 0, size.width, size.height);
        UIColor *color = DefaultWaterColor;
        circle.backgroundColor = color.CGColor;
        circle.anchorPoint = CGPointMake(0.5f, 0.5f);
        circle.opacity = 1.0f;
        circle.cornerRadius = circle.bounds.size.height / 2.0f;
        circle.transform = CATransform3DMakeScale(0.0f, 0.0f, 0.0f);
        
        CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
        transformAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.2f, 0.2f, 0.0f)];
        transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0f, 1.0f, 0.0f)];
        
        CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAnimation.fromValue = @(1.0f);
        opacityAnimation.toValue = @(0.0f);
        
        CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
        animationGroup.removedOnCompletion = NO;
        animationGroup.beginTime = beginTime + i * 0.15f;
        animationGroup.repeatCount = HUGE_VALF;
        animationGroup.duration = 1.2f;
        animationGroup.animations = @[transformAnimation, opacityAnimation];
        
        [animatedView.layer addSublayer:circle];
        [circle addAnimation:animationGroup forKey:@"animation"];
    }
}

@end
